/* **************************************************************************** *
* Sierra Leone - Social Incentives for Childhood Immunization                  *
* Effects of Signals on Timely Vaccination - Vaccine 4 & 5:  Different Cutoffs *
* **************************************************************************** *

** Purpose:     Effects of Signals on Timely Vaccination - Vaccine 4 & 5: Different Cutoffs

*/
* **************************************************************************** *


* **************************************************************************** *
* 1.) Generate  a data set with the outcomes constructed according to timeliness
* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


    * Keep only children that were at least 12 months age the last time we observed them:
    keep if age_pikin >= 365

    * Drop babies born in December 2017 and January 2018:
    keep if dob < td(01dec2017)


    * Generate Outcomes to vary the cutoffs:
    gen    penta3_5mo     = penta3
    gen    penta3_45mo    = penta3
    gen    penta3_6mo     = penta3

    gen    measles1_10mo  = measles1
    gen    measles1_11mo  = measles1
    gen    measles1_115mo = measles1
    gen    measles1_12mo  = measles1

    summ   measles1 measles1_10mo measles1_11mo  measles1_115mo measles1_12mo


    * ----------------------------------------------------------------------------
    * Create a follow-up indicator for each vaccine based on the age of the child
    *   during the last listing:

    local    VaccineVars = " bcg penta1 penta2 penta3 penta3_5mo penta3_45mo penta3_6mo measles1  measles1_10mo measles1_11mo measles1_115mo measles1_12mo"

    foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .
    }


    replace age_bcg_mopup      = 0            if age_listing !=. & age_listing >= 91
    replace age_bcg_mopup      = 1            if age_listing !=. & age_listing <  91

    replace age_penta1_mopup   = 0            if age_listing !=. & age_listing >= 122
    replace age_penta1_mopup   = 1            if age_listing !=. & age_listing <  122

    replace age_penta2_mopup   = 0            if age_listing !=. & age_listing >= 152
    replace age_penta2_mopup   = 1            if age_listing !=. & age_listing <  152


    local    VaccineVars = " bcg penta1 penta2 penta3 penta3_5mo penta3_45mo penta3_6mo measles1  measles1_10mo measles1_11mo measles1_115mo measles1_12mo"

    foreach  num of numlist 45 5 6  {
      replace age_penta3_`num'mo_mopup = 0          if age_listing !=. & age_listing >= 183
      replace age_penta3_`num'mo_mopup = 1          if age_listing !=. & age_listing <  183
    }

    foreach  num of numlist 10 11 115 12  {
      replace age_measles1_`num'mo_mopup = 0       if age_listing !=. & age_listing >= 350
      replace age_measles1_`num'mo_mopup = 1       if age_listing !=. & age_listing <  350
    }



* ------------------------------------------------------------------------------
* Construct outcomes according to timeliness

  * Save the vacine outcomes in seperate variables that will be used to code
  *      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen    `vaccine'_raw = `vaccine'
	}

	* Code Vaccine 1 outcome
	local a = 91

	foreach var in bcg {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}

	* Code Vaccine 2 outcome
	local b = 122

	foreach var in penta1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`b' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`b' & age_pikin!=.
		replace `var' = .          if age_pikin<`b'

		replace `var' = 0          if bcg ==0 & `var'==1
	}


	* Code Vaccine 3 outcome
	local c = 152

	foreach  var in penta2 {
		replace `var' = 0          if `var'==1 & age_`var'>=`c' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`c' & age_pikin!=.
		replace `var' = .          if age_pikin<`c'

		replace `var' = 0          if penta1 ==0 & `var'==1
	}


  * Vaccine 4 at 152 days - 5 months
	local d = 152

	foreach  var in penta3 {
		replace `var'_5mo = 0          if `var'_5mo==1 & age_`var'>=`d' & age_`var'!=.
		replace `var'_5mo = 0          if `var'_5mo==. & age_pikin>=`d' & age_pikin!=. 
		replace `var'_5mo = .          if age_pikin<`d'

		replace `var'_5mo = 0          if penta2 ==0 & `var'_5mo==1
	}



  * Vaccine 4 at 6 months as in main results
  local j = 183

  foreach  var in penta3 {
    replace `var' = 0          if `var'==1 & age_`var'>=`j' & age_`var'!=.
    replace `var' = 0          if `var'==. & age_pikin>=`j' & age_pikin!=. 
    replace `var' = .          if age_pikin<`j'

    replace `var' = 0          if penta2 ==0 & `var'==1
  }



  local e = 183

	foreach  var in penta3 {
		replace `var'_6mo = 0          if `var'_6mo==1 & age_`var'>=`e' & age_`var'!=.
		replace `var'_6mo = 0          if `var'_6mo==. & age_pikin>=`e' & age_pikin!=. 
		replace `var'_6mo = .          if age_pikin<`e'

		replace `var'_6mo = 0          if penta2 ==0 & `var'_6mo==1
	}



  * Penta 3 at 4.5 months as in main results
  local f = 137

  foreach  var in penta3 {
    replace `var'_45mo = 0          if `var'_45mo==1 & age_`var'>=`f' & age_`var'!=.
    replace `var'_45mo = 0          if `var'_45mo==. & age_pikin>=`f' & age_pikin!=. 
    replace `var'_45mo = .          if age_pikin<`f'

    replace `var'_45mo = 0          if penta2 ==0 & `var'_45mo==1
  }



	* Code Vaccine 5 outcome
  * at 350 days - 11.5 months as in main results
  local g = 350

	foreach var in measles1 {
		replace `var'_115mo = 0          if `var'==1&age_`var'>=`g'&age_`var'!=.
		replace `var'_115mo = 0          if `var'==.&age_pikin>=`g'&age_pikin!=.
		replace `var'_115mo = .          if age_pikin<`g'

		replace `var'_115mo = 0          if penta3 ==0&`var'_115mo==1
	}

  local h = 304

	foreach var in measles1 {
		replace `var'_10mo = 0          if `var'_10mo==1&age_`var'>=`h'&age_`var'!=.
		replace `var'_10mo = 0          if `var'_10mo==.&age_pikin>=`h'&age_pikin!=.
		replace `var'_10mo= .           if age_pikin<`h'

		replace `var'_10mo = 0          if penta3 ==0&`var'_10mo==1
	}

  local i = 335

	foreach var in measles1 {
		replace `var'_11mo = 0          if `var'_11mo==1&age_`var'>=`i'&age_`var'!=.
		replace `var'_11mo = 0          if `var'_11mo==.&age_pikin>=`i'&age_pikin!=.
		replace `var'_11mo= .           if age_pikin<`i'

		replace `var'_11mo = 0          if penta3 ==0&`var'_11mo==1
	}

  local j = 365

  foreach var in measles1 {
    replace `var'_12mo = 0          if `var'_12mo==1&age_`var'>=`j'&age_`var'!=.
    replace `var'_12mo = 0          if `var'_12mo==.&age_pikin>=`j'&age_pikin!=.
    replace `var'_12mo= .           if age_pikin<`j'

    replace `var'_12mo = 0          if penta3 ==0&`var'_12mo==1
  }


  summ   penta3_6mo penta3_5mo penta3_45mo
  summ   measles1_10mo measles1_11mo measles1_115mo measles1_12mo if intervention_arm==1


  * ----------------------------------------------------------------------------
  * Demean control variables for this sample:

	replace good_memory = 2          if good_memory ==.

	tab     good_memory, gen(recall)

	tab     arm_anc, gen(anc)

	foreach var in age_listing distance clinic_pop anc2 anc3 anc4 recall1 recall2 {
		sum  `var'  if intervention_arm==1, detail
		gen  `var'_dm    = `var' - `r(mean)'
	}


	summ   bcg penta1 penta2 penta3_45mo penta3_5mo penta3_6mo measles1_10mo measles1_11mo  measles1_115mo measles1_12mo

	lab var distance_dm "Distance"


* ******************************************************************************
* Run regressions: Generate table
* ******************************************************************************

  local ANC_dm      = " anc2_dm anc3_dm anc4_dm  "
  local Controls_dm = " recall1_dm recall2_dm distance_dm clinic_pop_dm"

  local VaccineList = " penta3_5mo penta3_45mo penta3_6mo measles1_10mo measles1_11mo measles1_115mo measles1_12mo"

  eststo clear
  foreach vaccine of local VaccineList {
	areg    `vaccine'  treat3 treat4 treat2   age_`vaccine'_mopup `Controls_dm'  `ANC_dm', absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat2 = treat3
	estadd scalar treat2_treat3 = r(p)
	test   treat2 = treat4
	estadd scalar treat2_treat4 = r(p)
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	test   treat2 treat3 treat4
	estadd scalar bracelets     = r(p)
	estadd local Obs            = "`e(N)'"
	estadd scalar C_mean        = _b[_cons]
	estadd local controls     "Yes"
	eststo model_`vaccine'
    }

	esttab  ///
	model_penta3_45mo model_penta3_5mo    model_penta3_6mo    ///
	model_measles1_10mo model_measles1_11mo model_measles1_115mo model_measles1_12mo ///
	using "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex", ///
	prehead("\begin{tabular}{l*{9}{c}} \toprule \\"                          ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{3}{l}{\textbf{4 Vaccines}}                     & \multicolumn{4}{l}{\textbf{5 Vaccines}}     \\"           ///
	"\multicolumn{1}{l}{Age cutoff}              & \multicolumn{1}{c}{4.5 months}      & \multicolumn{1}{c}{5 months} & \multicolumn{1}{c}{6 months} & \multicolumn{1}{c}{10 months}  & \multicolumn{1}{c}{11 months} & \multicolumn{1}{c}{11.5 months} & \multicolumn{1}{c}{12 months}  \\")   ///
	scalars("C_mean         Control Group mean"                           ///
	"Obs            Observations"                                 ///
	"treat2_treat3  \(S_{4}\) \(>\) 0: p(UI = S4)"                ///
	"treat2_treat4  \(S_{5}\) \(>\) 0: p(UI = S5)"                ///
	"treat3_treat4  p(S4 = S5)"                                   ///
	"bracelets      Joint F-Test"                                 ///
	"controls       Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	keep(_cons treat3 treat4 treat2 distance_dm)                          ///
	varlabels(_cons "Control Group mean")                                 ///
	sfmt(3) ${StarsOpt}   nomtitles                                       ///
	addnotes("") label b(3) se(3)  nobaselevels noconstant noobs          ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex  ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_Robustness.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"


	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls


	esttab  ///
	model_penta3_45mo model_penta3_5mo    model_penta3_6mo    ///
	model_measles1_10mo model_measles1_11mo model_measles1_115mo model_measles1_12mo ///
	using "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex", ///
	prehead("\begin{tabular}{l*{9}{c}} \toprule \\"                          ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{3}{l}{\textbf{4 Vaccines}}                     & \multicolumn{4}{l}{\textbf{5 Vaccines}}     \\"           ///
	"\multicolumn{1}{l}{Age cutoff}              & \multicolumn{1}{c}{4.5 months}      & \multicolumn{1}{c}{5 months} & \multicolumn{1}{c}{6 months} & \multicolumn{1}{c}{10 months}  & \multicolumn{1}{c}{11 months} & \multicolumn{1}{c}{11.5 months} & \multicolumn{1}{c}{12 months}  \\")   ///
	scalars("C_mean         Control Group mean"                           ///
	"Obs            Observations"                                 ///
	"treat2_treat3  \(S_{4}\) \(>\) 0: p(UI = S4)"                ///
	"treat2_treat4  \(S_{5}\) \(>\) 0: p(UI = S5)"                ///
	"treat3_treat4  p(S4 = S5)"                                   ///
	"bracelets      Joint F-Test"                                 ///
	"controls       Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	keep(_cons treat3 treat4 treat2 distance_dm)                          ///
	varlabels(_cons "Control Group mean")                                 ///
	sfmt(3) ${StarsOpt}   nomtitles                                       ///
	addnotes("") label b(3) p(3)  nobaselevels noconstant noobs          ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex  ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/pvalues/Table_EffectsOfTimely_Robustness_pvals.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfTimely_raw.tex"




* ******************************************************************************
* End of the Dofile !!!
* ******************************************************************************
